package com.javasoft.service.ZSW;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.javasoft.dao.ZSW.GetMaxScoreDao;
import com.javasoft.dao.ZSW.PersonalExamAnalysisDao;
import com.javasoft.entity.ZSW.GetMaxScoreEntity;
import com.javasoft.entity.ZSW.PersonalExamAnalysisEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.stream.IntStream;

@Service("personalExamAnalysisService")
public class PersonalExamAnalysisServiceImpl implements PersonalExamAnalysisService{

    @Resource
    private PersonalExamAnalysisDao personalExamAnalysisDao;
    @Resource
    private GetMaxScoreDao getMaxScoreDao;
    @Override
    public String analysis(Integer studentId, Integer examId) {
        PersonalExamAnalysisEntity entity1 = personalExamAnalysisDao.findscore(studentId, examId);
        GetMaxScoreEntity entity2 = getMaxScoreDao.findscore(examId);
        if (entity1!= null) {
            int correctNumber=countTotalJudgeOnes();
            int a=entity1.getTotalScore();//学生得分
            int b=entity2.getTotalScore();//考试总分
            if(a<0.6*b) {
                return ("当前试卷您的得分为：" + a+"，不合格,您一共答对了："+correctNumber+"题。");
            }
            if(a>0.6*b&&a<0.9*b){
                return ("当前试卷您的得分为：" + a+"，中等,您一共答对了："+correctNumber+"题。");
            }
            if(a>0.9*b){
                return ("当前试卷您的得分为：" + a+"，优秀,您一共答对了："+correctNumber+"题。");
            }
        }
        return "未找到您的成绩"; // 如果没查到对应记录
    }


    public int countTotalJudgeOnes() {
        final int[] totalCount = {0};

        // 使用IntStream循环遍历1到30，对应judge1到judge30字段
        IntStream.rangeClosed(1, 30).forEach(i -> {
            String fieldName = "judge" + i;
            // 使用QueryWrapper构建查询条件，统计每个judge字段值为1的记录数
            QueryWrapper<PersonalExamAnalysisEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(fieldName, "1");
            int count = personalExamAnalysisDao.selectCount(queryWrapper);
            totalCount[0] += count;
        });

        return totalCount[0];
    }
}
